//
//  SYKSXuanZeXueShengView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/27.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYKSXuanZeXueShengView.h"
#import "SYKaoShiSelectTableViewCell.h"


@interface SYKSXuanZeXueShengView () <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *contView;

@property (weak, nonatomic) IBOutlet UITableView *curTableView;

@property (strong, nonatomic) SYKSXuanZeXueShengView *xueshengView;

@property (strong, nonatomic) NSMutableArray *dataArr;

@property (strong, nonatomic) NSMutableArray *selectArr;//选中得arr

@end




@implementation SYKSXuanZeXueShengView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.contView.layer.masksToBounds = YES;
    self.contView.layer.cornerRadius = 5;
    self.curTableView.delegate = self;
    self.curTableView.dataSource = self;
    [_curTableView registerCellWithNIbNames:@[@"SYKaoShiSelectTableViewCell"]];
    _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _selectArr = [NSMutableArray array];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYKaoShiSelectTableViewCell *xueCell = [tableView dequeueReusableCellWithIdentifier:@"SYKaoShiSelectTableViewCell" forIndexPath:indexPath];
    xueCell.xueshengModel = self.dataArr[indexPath.row];
    xueCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return xueCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.isFrom isEqualToString:@"chat"]) {
        
        SYExamInfoStudentsModel *studentsModel = self.dataArr[indexPath.row];
        studentsModel.newisSelect = !studentsModel.newisSelect;
        
        for (SYExamInfoStudentsModel *stud in self.dataArr) {
            
            if (![stud.studentId isEqualToString:studentsModel.studentId]) {
                
                stud.newisSelect = NO;
            }
        }
        [self.curTableView reloadData];
        
    }else{
        
        SYExamInfoStudentsModel *studentsModel = self.dataArr[indexPath.row];
        studentsModel.newisSelect = !studentsModel.newisSelect;
        [self.curTableView reloadData];
    }

    
}

//取消
- (IBAction)quxiaoBtnClick:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    
    for (SYExamInfoStudentsModel *model in self.dataArr) {
        model.newisSelect = model.isSelect;
        
    }
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.xueshengView.frame = CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        [weakSelf.xueshengView removeFromSuperview];
    }];
    
}
//确定
- (IBAction)quedingBtnClick:(UIButton *)sender {
    NSString *studentIds = nil;
    NSString *nameStr = nil;
    
    
    for (SYExamInfoStudentsModel *model in self.dataArr) {
        model.isSelect = model.newisSelect;
        if (model.isSelect == YES) {
            if (studentIds.length == 0) {
                nameStr = model.studentName;
                studentIds = model.studentId;
            } else {
                studentIds = [NSString stringWithFormat:@"%@,%@",studentIds,model.studentId];
            }
        }
    }
    
    if (studentIds.length == 0 || nameStr == 0) {
        [MBProgressHUD zr_showError:@"请选择学生"];
        return;
    }
    
    
    if (self.block) {
        self.block(studentIds, nameStr);
    }
    
    
    [self quxiaoBtnClick:sender];
    
    
    
    
}
//全选
- (IBAction)quanxuanBtnClick:(UIButton *)sender {
    
    if ([self.isFrom isEqualToString:@"chat"]) {
        
        [MBProgressHUD zr_showError:@"不支持全选"];
        return;
    }
    [self.selectArr removeAllObjects];
    for (SYExamInfoStudentsModel *model in self.dataArr) {
        model.newisSelect = YES;
        
    }
    [self.curTableView reloadData];
}


- (void)showKaoShiBanJi:(SYKSXuanZeXueShengView *)kaoshiView dataArr:(NSMutableArray *)dataArr; {
    _xueshengView = kaoshiView;
    _dataArr = dataArr;
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    kaoshiView.frame = CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT);
    [window addSubview:kaoshiView];
    [UIView animateWithDuration:kAnimate animations:^{
        kaoshiView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        
    }];
    [self.curTableView reloadData];
    
}


@end
























